package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.SoftBevelBorder;

import network.Portas;

import listeners.EnviarListener;
import listeners.ErroListener;
import listeners.MenuListener;
import listeners.PauseListener;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class MainRecebimento extends JFrame {

	{
		// Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager
					.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private JPanel jPanelPrincipal;
	private static JPanel jPanelMatriz;
	private JPanel jPanelConsole;
	private static JPanel[][] Matriz = new JPanel[20][20];
	private int numPacotes = 0;
	private static int i = 0;
	private static int j = 0;
	public static JTextArea jTextAreaConsole;
	public static FileOutputStream fos = null;

	private void initGUI() {
		try {
			{

				this.setResizable(false);
				this.setVisible(true);
				this.setDefaultCloseOperation(EXIT_ON_CLOSE);
				this.setSize(800, 600);
				this.setTitle("Trabalho de Redes NP1");
				GroupLayout thisLayout = new GroupLayout(
						(JComponent) getContentPane());
				getContentPane().setLayout(thisLayout);
				this.setLocationRelativeTo(null);
				{
					jPanelPrincipal = new JPanel();
					GroupLayout jPanelPrincipalLayout = new GroupLayout(
							(JComponent) jPanelPrincipal);
					jPanelPrincipal.setLayout(jPanelPrincipalLayout);
					{
						jPanelMatriz = new JPanel();
						GridLayout jPanelMatrizLayout = new GridLayout(20, 20);
						jPanelMatrizLayout.setColumns(20);
						jPanelMatrizLayout.setHgap(8);
						jPanelMatrizLayout.setVgap(5);
						jPanelMatrizLayout.setRows(20);
						jPanelMatriz.setLayout(jPanelMatrizLayout);
						jPanelMatriz.setBorder(new SoftBevelBorder(
								BevelBorder.LOWERED, null, null, null, null));
						jPanelMatriz.setBackground(new java.awt.Color(128, 128,
								128));
					}
					{
						jPanelConsole = new JPanel();
						jPanelConsole.setBorder(new SoftBevelBorder(
								BevelBorder.LOWERED, null, null, null, null));
						jPanelConsole.setBackground(new java.awt.Color(255,
								255, 255));
						{
							jTextAreaConsole = new JTextArea();
							jPanelConsole.add(jTextAreaConsole);
							jTextAreaConsole.setPreferredSize(new java.awt.Dimension(650, 216));
						}

					}
					jPanelPrincipalLayout
							.setVerticalGroup(jPanelPrincipalLayout
									.createSequentialGroup()
									.addContainerGap()
									.addComponent(jPanelMatriz,
											GroupLayout.PREFERRED_SIZE, 317,
											GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(
											LayoutStyle.ComponentPlacement.RELATED)
									.addComponent(jPanelConsole, 0, 196,
											Short.MAX_VALUE).addContainerGap());
					jPanelPrincipalLayout
							.setHorizontalGroup(jPanelPrincipalLayout
									.createSequentialGroup()
									.addContainerGap()
									.addGroup(
											jPanelPrincipalLayout
													.createParallelGroup()
													.addComponent(
															jPanelMatriz,
															GroupLayout.Alignment.LEADING,
															GroupLayout.PREFERRED_SIZE,
															658,
															GroupLayout.PREFERRED_SIZE)
													.addComponent(
															jPanelConsole,
															GroupLayout.Alignment.LEADING,
															GroupLayout.PREFERRED_SIZE,
															658,
															GroupLayout.PREFERRED_SIZE))
									.addPreferredGap(
											LayoutStyle.ComponentPlacement.RELATED,
											16, GroupLayout.PREFERRED_SIZE));
				}
				thisLayout
						.setVerticalGroup(thisLayout.createSequentialGroup()
								.addComponent(jPanelPrincipal, 0, 541,
										Short.MAX_VALUE));
				thisLayout.setHorizontalGroup(thisLayout
						.createSequentialGroup().addComponent(jPanelPrincipal,
								0, 784, Short.MAX_VALUE));
			}
			{
				construirMatriz();
				Portas.abrirCom2();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new MainRecebimento().initGUI();
		String strFilePath = "D://demo.txt";
		try {
			fos = new FileOutputStream(strFilePath);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void construirMatriz() {
		JPanel aux;
		for (int i = 0; i < 20; i++) {
			for (int j = 0; j < 20; j++) {
				aux = new JPanel();
				aux.setBackground(Color.white);
				jPanelMatriz.setBackground(Color.white);
				Matriz[i][j] = aux;
				jPanelMatriz.add(aux);
			}
		}
	}
	public static void pintaPacote(Color c) {

			Matriz[i][j].setBackground(c);
			Matriz[i][j].setBorder(new LineBorder(Color.black));
			if(j<20){
				j++;
			}else{
				j=0;
				i++;
			}
		jPanelMatriz.updateUI();
	}
	public static void printConsole(String mensagem){
		jTextAreaConsole.setText(jTextAreaConsole.getText()+"\n"+mensagem);
	}

}
